from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.EmploymentAdminController import EmploymentAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import QueryBase
from schema.TeachingSchema import EmploymentInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

employment_admin_router = APIRouter()
@employment_admin_router.post("/employment/add", summary='添加就业信息')
def add_employment(employmentInfo: EmploymentInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employment/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employment/add', db):
            response = EmploymentAdminController.add_employment(employmentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employment_admin_router.post("/employment/mod", summary='根据就业信息id修改就业信息')
def mod_employment(employmentInfo: EmploymentInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employment/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employment/mod', db):
            response = EmploymentAdminController.mod_employment(employmentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employment_admin_router.get("/employment/query/{student_id}", summary='根据学员id查询就业信息')
def query_employment_by_student_id(student_id: int, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employment/query/student_id={student_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employment/query', db):
            response = EmploymentAdminController.query_employment_by_student_id(student_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employment_admin_router.post("/employment/querybypage", summary='分页查询所有就业信息')
def query_employment_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employment/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employment/query', db):
            response = EmploymentAdminController.query_employment_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result

